package com.codename1.samples;


import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.testing.AbstractTest;
import com.codename1.ui.Button;
import com.codename1.ui.Container;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.LayeredLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class SpanLabelLayeredLayoutPreferredSizeTest3000 {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Button showPopUp = new Button("Show PopUp in Border Layout");
        Form f = new Form(BoxLayout.y());
        //f.setAllowEnableLayoutOnPaint(true);
        f.add(showPopUp);
        
        showPopUp.addActionListener((e) -> {
            
            SpanLabel messageSpanLabel = new SpanLabel("Tap the following button to open the gallery. You should be able to select multiple images and videos.");
            
            Container centerContainerOuter = new Container(new BorderLayout(CENTER_BEHAVIOR_CENTER));
            centerContainerOuter.add(CENTER, messageSpanLabel);

            Container layeredPane = getCurrentForm().getLayeredPane();
            layeredPane.setLayout(new LayeredLayout());        
            layeredPane.add(centerContainerOuter);
            layeredPane.setVisible(true);

            getCurrentForm().revalidate();     
        });
        
        Button showPopUp2 = new Button("Show PopUp in Layered Layout");
        f.add(showPopUp2);
        showPopUp2.addActionListener((e) -> {
            
            SpanLabel messageSpanLabel = new SpanLabel("Tap the following button to open the gallery. You should be able to select multiple images and videos.");
            
            

            Container layeredPane = getCurrentForm().getLayeredPane();
            layeredPane.setLayout(new LayeredLayout());        
            layeredPane.add(messageSpanLabel);
            LayeredLayout ll = (LayeredLayout)layeredPane.getLayout();
            ll.setInsets(messageSpanLabel, "auto");
            layeredPane.setVisible(true);

            getCurrentForm().revalidate();     
        });
        
        Button runUnitTest = new Button("Run Unit Test");
        runUnitTest.addActionListener(evt->{
            try {
                UnitTest t = new UnitTest();
                t.runTest();
                Dialog.show("Test Passed", "Test Passed", "OK", null);
                
            } catch (Exception ex) {
                   Log.e(ex);
                   Dialog.show("Test failed", "Test Failed", "OK", null);
             }
        });
        f.add(runUnitTest);
        f.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }
    
    public class UnitTest extends AbstractTest {

        @Override
        public boolean shouldExecuteOnEDT() {
            return true;
        }

        
        private void testBorderLayout() {
            Button showPopUp = new Button("Show PopUp in Border Layout");
            Form f = new Form(BoxLayout.y());
            f.setName("testBorderLayout");
            f.add(showPopUp);

            showPopUp.addActionListener((e) -> {

                SpanLabel messageSpanLabel = new SpanLabel("Tap the following button to open the gallery. You should be able to select multiple images and videos. Tap the following button to open the gallery. You should be able to select multiple images and videos.");
                messageSpanLabel.setName("messageSpanLabel");
                Container centerContainerOuter = new Container(new BorderLayout(CENTER_BEHAVIOR_CENTER));
                centerContainerOuter.add(CENTER, messageSpanLabel);

                Container layeredPane = getCurrentForm().getLayeredPane();
                layeredPane.setLayout(new LayeredLayout());        
                layeredPane.add(centerContainerOuter);
                layeredPane.setVisible(true);

                getCurrentForm().revalidate();     
            });
            showPopUp.setName("showBorderLayout");
            f.show();
            waitForFormName("testBorderLayout");
            clickButtonByName("showBorderLayout");
            SpanLabel spanLabel = (SpanLabel)findByName("messageSpanLabel");
            Label l = new Label("Tap the following");
            
            assertTrue(spanLabel.getHeight() > l.getPreferredH() * 2, "Span Label height is too small.  Should be at least a few lines.");
            
            
        }
        
        
        @Override
        public boolean runTest() throws Exception {
            testBorderLayout();
            return true;
        }
        
    }

}
